const webview = document.getElementById('external-webview');

webview.addEventListener('did-finish-load', () => {
  console.log('Webview 加载完成');
});
webview.addEventListener('dom-ready', () => {
  console.log('Webview DOM 加载完成');
  const preloadScript = `
    (function () {
      const originalFetch = window.fetch;
      window.fetch = function () {
      debugger
        const args = arguments;
        const requestInfo = {
          method: args[0] && args[0].method,
          url: args[0] && args[0].url
        };
        ipcRenderer.send('requestInfo', requestInfo);
        return originalFetch.apply(this, args).then((response) => {
          const responseInfo = {
            status: response.status,
            url: response.url
          };
          ipcRenderer.send('responseInfo', responseInfo);
          return response;
        });
      };
      const originalXHR = XMLHttpRequest.prototype.open;
      XMLHttpRequest.prototype.open = function () {
        const args = arguments;
        const requestInfo = {
          method: args[0],
          url: args[1]
        };
        ipcRenderer.send('requestInfo', requestInfo);
        this.addEventListener('load', () => {
          const responseInfo = {
            status: this.status,
            url: this.responseURL
          };
          ipcRenderer.send('responseInfo', responseInfo);
        });
        return originalXHR.apply(this, args);
      };
    })();
  `;
  webview.executeJavaScript(preloadScript);
});

